<?php
Yii::import('application.extensions.*');

class DefaultController extends InstallController
{
	public function actionIndex()
	{
		$this->render('index',$this->data);
	}

	public function actionEnvironment(){
        session_start();
        $_SESSION['install']='RKCMS';
	    if(isset($_POST['agree'])){
            Yii::app()->session['agree']=1;
            $this->data['agree'] = 1;
        }
	    if($this->data['agree'] == 1){
            $this->render('environment',$this->data);
        }else{
	        $this->data['message'] = "必须同意安装协议！";
	        $this->actionIndex();
        }
    }

    public function actionInformation(){
        if(Yii::app()->session['environment'] == 1){
            $this->render('information',$this->data);
        }else{
            $this->data['message'] = "必须检测安装环境！";
            $this->actionEnvironment();
        }
    }

    public function actionTestdatabase(){
	    $host = $_REQUEST['host'];
        $port = $_REQUEST['port'];
        $database = $_REQUEST['database'];
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        try{
            $dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$database.';autoReconnect=true';
            $connection=new CDbConnection($dsn,$username,$password);
            $connection->active=true;
            echo json_encode(array('code'=>10000));
        }catch (Exception $exception){
            echo json_encode(array('code'=>10001));
        }
    }

    public function actionInstall(){
        $host = $_REQUEST['host'];
        $port = $_REQUEST['port'];
        $database = $_REQUEST['database'];
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        try{
            $manager = $_REQUEST['manager'];
            $this->data['manager'] = $manager;
            $loginpass = $_REQUEST['loginpass'];
            $this->data['loginpass'] = $loginpass;

            Yii::app()->session['manager'] = $manager;
            Yii::app()->session['loginpass'] = $loginpass;

            $dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$database.';autoReconnect=true';
            $connection=new CDbConnection($dsn,$username,$password);
            $connection->active=true;

            Yii::app()->session['dsn'] = $dsn;
            Yii::app()->session['username'] = $username;
            Yii::app()->session['password'] = $password;

            //整理SQL集合
            $documentRoot = $_SERVER['DOCUMENT_ROOT'];
            $filePath = $documentRoot.'/themes/install/install.sql';
            $sqllist = Sql::getSqlFromFile($filePath);
            $sqlInsertAdmin = "insert into xm_user (loginname,loginpass) values ('".$manager."','".md5($loginpass)."')";
            array_push($sqllist,$sqlInsertAdmin);
            $this->data['sqllist'] = $sqllist;
            Yii::app()->session['sqllist'] = $sqllist;

            $this->render('install',$this->data);

        }catch (Exception $exception){
            $this->data['message'] = "数据库配置错误！";
            $this->actionInformation();
        }
    }

    public function actionQuerysql(){
        $sqllist = Yii::app()->session['sqllist'];
        try {
            $dsn = Yii::app()->session['dsn'];
            $username = Yii::app()->session['username'];
            $password = Yii::app()->session['password'];
            $connection = new CDbConnection($dsn, $username, $password);
            $connection->active = true;

            foreach ($sqllist as $sql){
                $command=$connection->createCommand($sql);
                $command->execute();
            }
            echo json_encode(array('code'=>10000));
        }catch (Exception $exception){
            echo json_encode(array('code'=>10002));
        }
    }

    public function actionUpdatedbconfig(){

        try {
            $dsn = Yii::app()->session['dsn'];
            $username = Yii::app()->session['username'];
            $password = Yii::app()->session['password'];

            $dbconfig = array(
                'connectionString' => $dsn,
                'emulatePrepare' => true,
                'username' => $username,
                'password' => $password,
                'charset' => 'utf8',
            );

            $documentRoot = $_SERVER['DOCUMENT_ROOT'];
            $filePath = "/common/database.php";
            $fp = fopen($documentRoot.$filePath,'w+');
            fwrite($fp,"<?php return ".var_export($dbconfig,true)." ?>");
            fclose($fp);

            $mainInstallFile = "/frontend/protected/config/main_install.php";
            $mainFile = "/frontend/protected/config/main.php";

            copy($documentRoot.$mainInstallFile,$documentRoot.$mainFile);

            echo json_encode(array('code'=>10000));
        }catch (Exception $exception){
            echo json_encode(array('code'=>10003));
        }

    }

    public function actionLockinstall(){
        try {
            $documentRoot = $_SERVER['DOCUMENT_ROOT'];
            $filePath = "/common/install.lock";
            $fp = fopen($documentRoot.$filePath,'w+');
            fwrite($fp,"RKCMS-INSTALL LOCK");
            fclose($fp);
            echo json_encode(array('code'=>10000));
        }catch (Exception $exception){
            echo json_encode(array('code'=>10004));
        }
    }

    public function actionWelcome(){
        $this->render('welcome',$this->data);
    }


}